package org.gesplan.ui;

import java.awt.EventQueue;
import java.beans.Beans;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.swing.JFrame;
import org.gesplan.controller.GenericController;
import org.gesplan.controller.StaffJpaController;
import org.gesplan.entity.Staff;
import org.gesplan.exceptions.NonexistentEntityException;
import org.gesplan.listeners.LoggingBindingListener;
import org.gesplan.listeners.SelectionListener;
import org.gesplan.util.Utils;


/**
 *
 * @author David
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public class StaffPanel extends AbstractPanel {

    public StaffPanel() {
        super();
        try {
            initComponents();
        } catch (Exception e) {
            logger.log(Level.SEVERE, bundle.getString("ERROR_DB_CONNECTION"), e);
        }
        if (!Beans.isDesignTime()) {
            initConfig();
        }
    }


    private void initConfig() {

        super.abstractTable = masterTable;
        super.abstractQuery = query;
        super.abstractList = list;

        // Add Listeners
        LoggingBindingListener lbl = new LoggingBindingListener(saveButton);
        bindingGroup.addBindingListener(lbl);
        masterTable.getSelectionModel().addListSelectionListener(new SelectionListener(masterTable));

        // Autocomplete Support for combos
        GenericController.fillComboBox(bundle.getString("qryRol"), rolComboBox);
        GenericController.fillComboBox(bundle.getString("qrySection"), sectionComboBox);

        initSorter();
        setHeaders();

    }

    
    private void setHeaders(){
        Utils.setTableHeader(masterTable);
    }
    
    
    @Override
    protected void newRecord() {
        logger.info("Staff.new");
        Staff s = new Staff();
        s.setNameFull("<NOM>");
        entityManager.persist(s);
        list.add(s);
        super.newRecord();
    }


    @Override
    protected void save() {
        int selected = masterTable.getSelectedRow();
        if (selected > -1) {
            Staff o = list.get(masterTable.convertRowIndexToModel(selected));
            StaffJpaController dao = new StaffJpaController();
            try {
                dao.edit(o);
                logger.info("Staff.save: " + o.getId());
            } catch (NonexistentEntityException ex) {
                logger.severe(ex.getMessage());
            } catch (Exception ex) {
                logger.severe(ex.getMessage());
            }
        }
    }


    @Override
    protected void delete() {
        if (deleteConfirm()) {
            List<Staff> toRemove = new ArrayList<>(masterTable.getSelectedRows().length);
            StaffJpaController dao = new StaffJpaController();
            for (int idx = 0; idx < masterTable.getSelectedRows().length; idx++) {
                Staff s = list.get(masterTable.convertRowIndexToModel(masterTable.getSelectedRows()[idx]));
                try {
                    toRemove.add(s);
                    dao.destroy(s.getId());
                    logger.info("Staff.delete: " + s.getId());
                } catch (NonexistentEntityException ex) {
                    logger.log(Level.SEVERE, null, ex);
                }
            }
            list.removeAll(toRemove);
        }
    }


    // <editor-fold defaultstate="collapsed" desc="Auto Generated Code">    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        query = Beans.isDesignTime() ? null : entityManager.createQuery(bundle.getString("qryStaff")); // NOI18N;
        list = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : org.jdesktop.observablecollections.ObservableCollections.observableList(query.getResultList());
        sectionQuery = Beans.isDesignTime() ? null : entityManager.createQuery(bundle.getString("qrySection"));
        sectionList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : sectionQuery.getResultList();
        rolQuery = Beans.isDesignTime() ? null : entityManager.createQuery(bundle.getString("qryRol"));
        rolList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : rolQuery.getResultList();
        masterScrollPane = new javax.swing.JScrollPane();
        masterTable = new javax.swing.JTable();
        idLabel = new javax.swing.JLabel();
        dniLabel = new javax.swing.JLabel();
        nameFullLabel = new javax.swing.JLabel();
        dateStartLabel = new javax.swing.JLabel();
        dateEndLabel = new javax.swing.JLabel();
        sectionIdLabel = new javax.swing.JLabel();
        rolIdLabel = new javax.swing.JLabel();
        idField = new javax.swing.JTextField();
        dniField = new javax.swing.JTextField();
        nameFullField = new javax.swing.JTextField();
        saveButton = new javax.swing.JButton();
        refreshButton = new javax.swing.JButton();
        newButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        sectionComboBox = new javax.swing.JComboBox();
        rolComboBox = new javax.swing.JComboBox();
        startDateChooser = new com.toedter.calendar.JDateChooser();
        endDateChooser = new com.toedter.calendar.JDateChooser();
        previousButton = new javax.swing.JButton();
        nextButton = new javax.swing.JButton();

        FormListener formListener = new FormListener();

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, list, masterTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${id}"));
        columnBinding.setColumnName("Id");
        columnBinding.setColumnClass(Short.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${dni}"));
        columnBinding.setColumnName("Dni");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nameFull}"));
        columnBinding.setColumnName("Name Full");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${dateStart}"));
        columnBinding.setColumnName("Date Start");
        columnBinding.setColumnClass(java.util.Date.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${sectionId}"));
        columnBinding.setColumnName("Section Id");
        columnBinding.setColumnClass(org.gesplan.entity.Section.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${rolId}"));
        columnBinding.setColumnName("Rol Id");
        columnBinding.setColumnClass(org.gesplan.entity.Rol.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        masterTable.addPropertyChangeListener(formListener);
        masterScrollPane.setViewportView(masterTable);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/gesplan/properties/Bundle"); // NOI18N
        masterTable.getColumnModel().getColumn(0).setResizable(false);
        masterTable.getColumnModel().getColumn(0).setPreferredWidth(20);
        masterTable.getColumnModel().getColumn(0).setHeaderValue(bundle.getString("StaffPanel.masterTable.columnModel.title0")); // NOI18N
        masterTable.getColumnModel().getColumn(1).setResizable(false);
        masterTable.getColumnModel().getColumn(1).setPreferredWidth(40);
        masterTable.getColumnModel().getColumn(1).setHeaderValue(bundle.getString("StaffPanel.masterTable.columnModel.title1")); // NOI18N
        masterTable.getColumnModel().getColumn(2).setPreferredWidth(150);
        masterTable.getColumnModel().getColumn(2).setHeaderValue(bundle.getString("StaffPanel.masterTable.columnModel.title2")); // NOI18N
        masterTable.getColumnModel().getColumn(3).setHeaderValue(bundle.getString("StaffPanel.masterTable.columnModel.title3")); // NOI18N
        masterTable.getColumnModel().getColumn(4).setHeaderValue(bundle.getString("StaffPanel.masterTable.columnModel.title4")); // NOI18N
        masterTable.getColumnModel().getColumn(5).setHeaderValue(bundle.getString("StaffPanel.masterTable.columnModel.title5")); // NOI18N

        idLabel.setText(bundle.getString("StaffPanel.idLabel.text")); // NOI18N

        dniLabel.setText(bundle.getString("StaffPanel.dniLabel.text")); // NOI18N

        nameFullLabel.setText(bundle.getString("StaffPanel.nameFullLabel.text")); // NOI18N
        nameFullLabel.setToolTipText(bundle.getString("StaffPanel.nameFullLabel.toolTipText")); // NOI18N

        dateStartLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        dateStartLabel.setLabelFor(startDateChooser);
        dateStartLabel.setText(bundle.getString("StaffPanel.dateStartLabel.text")); // NOI18N

        dateEndLabel.setText(bundle.getString("StaffPanel.dateEndLabel.text")); // NOI18N

        sectionIdLabel.setLabelFor(sectionComboBox);
        sectionIdLabel.setText(bundle.getString("StaffPanel.sectionIdLabel.text")); // NOI18N

        rolIdLabel.setLabelFor(rolComboBox);
        rolIdLabel.setText(bundle.getString("StaffPanel.rolIdLabel.text")); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.id}"), idField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), idField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.dni}"), dniField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), dniField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        nameFullField.setName(""); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.nameFull}"), nameFullField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), nameFullField, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        saveButton.setMnemonic('G');
        saveButton.setText(bundle.getString("SAVE")); // NOI18N
        saveButton.setToolTipText(bundle.getString("StaffPanel.saveButton.toolTipText")); // NOI18N
        saveButton.addActionListener(formListener);

        refreshButton.setMnemonic('T');
        refreshButton.setText(bundle.getString("REFRESH")); // NOI18N
        refreshButton.setToolTipText(bundle.getString("StaffPanel.refreshButton.toolTipText")); // NOI18N
        refreshButton.addActionListener(formListener);

        newButton.setMnemonic('N');
        newButton.setText(bundle.getString("NEW")); // NOI18N
        newButton.addActionListener(formListener);

        deleteButton.setMnemonic('E');
        deleteButton.setText(bundle.getString("DELETE")); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), deleteButton, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        deleteButton.addActionListener(formListener);

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, sectionList, sectionComboBox, "bindSection");
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.sectionId}"), sectionComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), sectionComboBox, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, rolList, rolComboBox, "bindRol");
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.rolId}"), rolComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), rolComboBox, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        startDateChooser.setDateFormatString(bundle.getString("StaffPanel.startDateChooser.dateFormatString")); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.dateStart}"), startDateChooser, org.jdesktop.beansbinding.BeanProperty.create("date"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), startDateChooser, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        endDateChooser.setDateFormatString(bundle.getString("StaffPanel.endDateChooser.dateFormatString")); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.dateEnd}"), endDateChooser, org.jdesktop.beansbinding.BeanProperty.create("date"));
        bindingGroup.addBinding(binding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, masterTable, org.jdesktop.beansbinding.ELProperty.create("${selectedElement != null}"), endDateChooser, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        previousButton.setMnemonic('A');
        previousButton.setText(bundle.getString("PREVIOUS")); // NOI18N
        previousButton.addActionListener(formListener);

        nextButton.setMnemonic('S');
        nextButton.setText(bundle.getString("NEXT")); // NOI18N
        nextButton.addActionListener(formListener);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(newButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(previousButton, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(nextButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(saveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(refreshButton)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(masterScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 652, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(nameFullLabel)
                                    .addComponent(idLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(sectionIdLabel)
                                    .addComponent(dateStartLabel))
                                .addGap(8, 8, 8)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(idField, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(dniLabel)
                                        .addGap(35, 35, 35)
                                        .addComponent(dniField, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(233, 233, 233))
                                    .addComponent(nameFullField, javax.swing.GroupLayout.PREFERRED_SIZE, 356, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(sectionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(startDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(dateEndLabel)
                                            .addComponent(rolIdLabel))
                                        .addGap(21, 21, 21)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(rolComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(endDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                        .addContainerGap(14, Short.MAX_VALUE))))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {newButton, refreshButton, saveButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(masterScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(idLabel)
                    .addComponent(idField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dniField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dniLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameFullLabel)
                    .addComponent(nameFullField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rolComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sectionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sectionIdLabel)
                    .addComponent(rolIdLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(dateEndLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(dateStartLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(endDateChooser, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
                    .addComponent(startDateChooser, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(refreshButton)
                    .addComponent(deleteButton)
                    .addComponent(newButton)
                    .addComponent(previousButton)
                    .addComponent(nextButton))
                .addContainerGap())
        );

        bindingGroup.bind();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener, java.beans.PropertyChangeListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == saveButton) {
                StaffPanel.this.saveButtonActionPerformed(evt);
            }
            else if (evt.getSource() == refreshButton) {
                StaffPanel.this.refreshButtonActionPerformed(evt);
            }
            else if (evt.getSource() == newButton) {
                StaffPanel.this.newButtonActionPerformed(evt);
            }
            else if (evt.getSource() == deleteButton) {
                StaffPanel.this.deleteButtonActionPerformed(evt);
            }
            else if (evt.getSource() == previousButton) {
                StaffPanel.this.previousButtonActionPerformed(evt);
            }
            else if (evt.getSource() == nextButton) {
                StaffPanel.this.nextButtonActionPerformed(evt);
            }
        }

        public void propertyChange(java.beans.PropertyChangeEvent evt) {
            if (evt.getSource() == masterTable) {
                StaffPanel.this.masterTablePropertyChange(evt);
            }
        }
    }// </editor-fold>//GEN-END:initComponents


    public static void main(String[] args) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) { //NOI18N
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(StaffPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(StaffPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(StaffPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(StaffPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setContentPane(new StaffPanel());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
    // </editor-fold>


    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        delete();
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        save();
    }//GEN-LAST:event_saveButtonActionPerformed

    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed
        refresh(query);
    }//GEN-LAST:event_refreshButtonActionPerformed

    private void previousButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_previousButtonActionPerformed
        previous();
    }//GEN-LAST:event_previousButtonActionPerformed

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextButtonActionPerformed
        next();
    }//GEN-LAST:event_nextButtonActionPerformed

    private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed
        newRecord();
    }//GEN-LAST:event_newButtonActionPerformed

    private void masterTablePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_masterTablePropertyChange
        Utils.setRecordInfo(masterTable);
    }//GEN-LAST:event_masterTablePropertyChange
    // <editor-fold defaultstate="collapsed" desc="Variables declaration">    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel dateEndLabel;
    private javax.swing.JLabel dateStartLabel;
    private javax.swing.JButton deleteButton;
    private javax.swing.JTextField dniField;
    private javax.swing.JLabel dniLabel;
    private com.toedter.calendar.JDateChooser endDateChooser;
    private javax.swing.JTextField idField;
    private javax.swing.JLabel idLabel;
    java.util.List<Staff> list;
    private javax.swing.JScrollPane masterScrollPane;
    private javax.swing.JTable masterTable;
    private javax.swing.JTextField nameFullField;
    private javax.swing.JLabel nameFullLabel;
    private javax.swing.JButton newButton;
    private javax.swing.JButton nextButton;
    private javax.swing.JButton previousButton;
    private javax.persistence.Query query;
    private javax.swing.JButton refreshButton;
    private javax.swing.JComboBox rolComboBox;
    private javax.swing.JLabel rolIdLabel;
    private java.util.List<org.gesplan.entity.Rol> rolList;
    private javax.persistence.Query rolQuery;
    private javax.swing.JButton saveButton;
    private javax.swing.JComboBox sectionComboBox;
    private javax.swing.JLabel sectionIdLabel;
    private java.util.List<org.gesplan.entity.Section> sectionList;
    private javax.persistence.Query sectionQuery;
    private com.toedter.calendar.JDateChooser startDateChooser;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    // </editor-fold>
}